package com.xhorse.lockplatform.comm.exception;

import com.xhorse.lockplatform.comm.model.Result;
import com.xhorse.lockplatform.comm.model.ResultUtil;
import com.xhorse.lockplatform.comm.util.LogUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Tom
 */
@ControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler({LockPlatFormException.class, Throwable.class})
    @ResponseBody
    public Result handle(Throwable e) {
        if (e instanceof LockPlatFormException) {
            LockPlatFormException loanPlatformException = (LockPlatFormException) e;
            return ResultUtil.failed(loanPlatformException.getCode(), loanPlatformException.getMessage());
        } else {
            LogUtil.getLogger().error("系统异常 {}", e);
            return ResultUtil.failed();
        }
    }
}
